package com.gaofei.myproject.controller;

import com.gaofei.myproject.mapper.DepartmentMapper;
import com.gaofei.myproject.mapper.EmployeeMapper;
import com.gaofei.myproject.pojo.Department;
import com.gaofei.myproject.pojo.Employee;
import com.gaofei.myproject.pojo.EmployeeWeb;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

@Controller
public class EmployeeController {
    @Autowired
    EmployeeMapper employeeMapper;
    @Autowired
    DepartmentMapper departmentMapper;

    //查出所有员工，并显示在页面上
    @RequestMapping("/employees")
    public String getEmployees(Model model){
        List<Employee> employees = employeeMapper.getEmployees();
        List<EmployeeWeb> list=new ArrayList();
        for (int i=0;i<employees.size();i++){

            EmployeeWeb employeeWeb=new EmployeeWeb();

            List<Department> depAll = departmentMapper.getDepartments();
            int department = employees.get(i).getDepartment();
            int id = employees.get(i).getId();
            String lastName = employees.get(i).getLastName();
            int gender = employees.get(i).getGender();
            String email = employees.get(i).getEmail();
            Date birth = employees.get(i).getBirth();
            for (int j=0;j<depAll.size();j++){

                int did = depAll.get(j).getDid();
                if (did==department){
                    employeeWeb.setDepartment(depAll.get(j));
                }
            }
            employeeWeb.setId(id);
            employeeWeb.setLastName(lastName);
            employeeWeb.setBirth(birth);
            employeeWeb.setEmail(email);
            employeeWeb.setGender(gender);
            list.add(employeeWeb);
        }

        model.addAttribute("emps",list);

        return "emp/list";
    }

    /*
    添加员工信息
     1.将员工职位名和did传到add页面的选项栏里
     2.将提交的表单添加到数据库里
     */
    @GetMapping("/emp")
    public String toAllpage(Model model){
        List<Department> depList =departmentMapper.getDepartments();
        model.addAttribute("departments",depList);
        return "emp/add";
    }
    @PostMapping("/emp")
    public String addEmp(Employee employee){
        employeeMapper.add(employee);
        return  "redirect:/employees";
    }


    //根据id删除员工
    @GetMapping("/delemp/{id}")
    public String delemp(@PathVariable("id")Integer id){

        employeeMapper.delete(id);
        return  "redirect:/employees";
    }


    @GetMapping("/toUpdate/{id}")
    public String toUpdate(@PathVariable("id")Integer id, Model model){
        System.out.println("toUpdate");
        Employee employee = employeeMapper.queryById(id);

        model.addAttribute("emp",employee);
        //查询所有部门信息
        List<Department> depAll = departmentMapper.getDepartments();
        model.addAttribute("departments",depAll);
        return "emp/update";
    }
    @PostMapping("/updateEmp")
    public String Update(Employee employee){
        employeeMapper.update(employee);
        return  "redirect:/employees";
    }



}
